var MesBonsDeCommande = {
    
    init: function(SubGrid) {
        
        /***********************************************************************
         * Variables
         **********************************************************************/
        var TableBonDeCommande = SubGrid.find('table#liste_bons_de_commande'),
        LinkCreate = TableBonDeCommande.find('a.create'),
        EditBonDeCommande = SubGrid.find('div#EditBonDeCommande');
        
        /***********************************************************************
         * Forms
         **********************************************************************/
        EditBonDeCommande.dialog({
            autoOpen: false,
            resizable: false,
            width: 800,
            height: 600,
            modal: true,
            buttons: {
                "Valider": function() {
                    var formData = {};
                    var COMMANDE_ID = EditBonDeCommande.find('input[type=hidden]#COMMANDE_ID').val();
                    var SOCIETE_ID = EditBonDeCommande.find('input[type=hidden]#SOCIETE_ID').val();
                    var COMMANDE_DATE = EditBonDeCommande.find('input[type=hidden]#COMMANDE_DATE').val();
                    var COMMANDE_CLIENT = EditBonDeCommande.find('input[type=hidden]#COMMANDE_CLIENT').val();
                    
                    var COMMANDE_DATE_MS = new Date().getTime();
                    COMMANDE_DATE = (COMMANDE_DATE != '') ? COMMANDE_DATE : Math.ceil(COMMANDE_DATE_MS / 1000);
                    
                    formData.COMMANDE_ID = COMMANDE_ID;
                    formData.SOCIETE_ID = SOCIETE_ID;
                    formData.COMMANDE_DATE = COMMANDE_DATE;
                    formData.COMMANDE_CLIENT = COMMANDE_CLIENT;
                    formData.COMMANDE_DETAILS = [];
                    
                    // lines in detail
                    var Fieldset_detail_commande = EditBonDeCommande.find('fieldset#detail_commande');
                    var TableDetailCommande = Fieldset_detail_commande.find('table#liste_details_commande');
                    var lines = TableDetailCommande.find('tr').not('thead tr');
                    $.each(lines, function (index, line){
                        var ARTICLE_ID = $(line).find('input[type=hidden][name="ARTICLE_ID"]').val();
                        var DETAIL_QUANTITE = $(line).find('input[type=text][name="DETAIL_QUANTITE"]').val();
                        formData.COMMANDE_DETAILS.push({ ARTICLE_ID: ARTICLE_ID, DETAIL_QUANTITE: DETAIL_QUANTITE });
                    });
                    
                    var url = EditBonDeCommande.find('form').attr('action');
                    
                    $.ajax({
                        type: "GET",
                        url: url,
                        data: formData,
                        async: false,
                        dataType: 'json',
                        success: function(bons_de_commande){
                            UpdateList(bons_de_commande);
                            EditBonDeCommande.dialog('close');
                        }
                    });
                },
                Cancel: function() {
                    EditBonDeCommande.dialog('close');
                }
            },
            beforeClose: function(event, ui) {
                var COMMANDE_DATE = EditBonDeCommande.find('input[type=text]#COMMANDE_DATE').val();
            },
            open: function(event, ui) {
                var Fieldset_detail_commande = EditBonDeCommande.find('fieldset#detail_commande');
                MesDetailsDeCommande.init(Fieldset_detail_commande);
            },
            close: function() {
                EditBonDeCommande.html('');
            }
        });
        
        /***********************************************************************
         * Methods on events
         **********************************************************************/
        function OnCreate(event, element) {
            event.preventDefault();
            var url = $(element).attr('href');
            EditBonDeCommande.load(url, function() {
                EditBonDeCommande.dialog('open');
            });
        }
    
        function UpdateList(bons_de_commandes) {
            var table = $('table#liste_bons_de_commande'),
            tbody = table.find('tbody'),
            base_url = $('input[type=hidden]#base_url').val();
        
            tbody.empty();

            $.each(bons_de_commandes, function (index, bon_de_commandes){
                var tr = '<tr id="' + bon_de_commandes.COMMANDE_ID + '">';
                tr = tr + '<td>' + bon_de_commandes.COMMANDE_ID + '</td>';
                tr = tr + '<td>' + bon_de_commandes.COMMANDE_DATE + '</td>';
                tr = tr + '<td></td>';
                tr = tr + '</tr>';

                table.append(tr);
            });
        }
        
        /***********************************************************************
         * Events
         **********************************************************************/
        LinkCreate.on('click', function(event) {
            OnCreate(event, $(this));
        });
        
    }
    
};